package org.fulmen.logger;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import java.util.regex.Pattern;

public final class RegexMatcher extends TypeSafeMatcher<String>
{
    private final Pattern pattern;


    public static final Matcher<String> regexMatch(final String regex)
    {
        return new RegexMatcher(Pattern.compile(regex));
    }

    RegexMatcher(final Pattern pattern)
    {
        this.pattern = pattern;
    }

    @Override
    protected boolean matchesSafely(final String item)
    {
        return pattern.matcher(item).matches();
    }

    @Override
    public void describeTo(final Description description)
    {
        description.appendText("regex match [").appendText(pattern.toString()).appendText("]");
    }
}
